from fastapi import FastAPI, Query
from typing import List
from datetime import datetime, timedelta

app = FastAPI(title="城市天气服务")

SUPPORTED_CITIES = ["北京", "上海", "广州", "深圳", "成都", "杭州"]

@app.get("/cities", response_model=List[str])
def get_cities():
    """查询支持的城市列表"""
    return SUPPORTED_CITIES

@app.get("/weather/today")
def get_today_weather(city: str = Query(..., description="城市名")):
    """查询指定城市的当天天气情况（模拟数据）"""
    if city not in SUPPORTED_CITIES:
        return {"error": "不支持该城市"}
    return {
        "city": city,
        "date": datetime.now().strftime("%Y-%m-%d"),
        "weather": "晴",
        "temperature": "22°C",
        "wind": "微风",
        "desc": f"{city}今日天气晴，气温适宜，适合出行。"
    }

@app.get("/weather/week")
def get_week_weather(city: str = Query(..., description="城市名")):
    """查询指定城市的未来一周天气情况（模拟数据）"""
    if city not in SUPPORTED_CITIES:
        return {"error": "不支持该城市"}
    today = datetime.now()
    week_data = []
    for i in range(7):
        day = today + timedelta(days=i)
        week_data.append({
            "date": day.strftime("%Y-%m-%d"),
            "weather": "晴" if i % 2 == 0 else "多云",
            "temperature": f"{20 + i}°C"
        })
    return {
        "city": city,
        "week_weather": week_data
    }
